package com.sprouting.ops.controller;

import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.model.DromaraFileStorage;
import com.sprouting.ops.model.OpsStakeholder;
import com.sprouting.ops.model.OpsUser;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.OpsStakeholderQo;
import com.sprouting.ops.service.DromaraFileStorageService;
import com.sprouting.ops.service.OpsStakeholderService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.UpdGroup;

/**
 * 干系人(OpsStakeholder)(OPS_STAKEHOLDER) 表控制层
 * todo 干系人  立场变化趋势（上升/持平/下降）  关键决策节点（时间点+事件）  反对原因/痛点
 * todo 风险和冲突关注点
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-09-01 16:53:16
 */
@Slf4j
@RestController
@RequestMapping("opsStakeholder")
public class OpsStakeholderController {

    @Resource
    private OpsStakeholderService opsStakeholderService;
    @Resource
    private DromaraFileStorageService dromaraFileStorageService;


    /**
     * 分页查询干系人
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(@Validated OpsStakeholderQo query){
        Page page = query.getPage();
        return ResultJson.success(opsStakeholderService.getListPage(page, query));
    }

    /**
     * 删除干系人
     * @param id 干系人id
     * @return
     */
    @PostMapping("delOpsStakeholder")
    @IdempotentSingle
    public ResultJson delOpsStakeholder(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        DromaraFileStorage unifiedFileInfo = dromaraFileStorageService.delFileBefore(id);
        opsStakeholderService.delOpsStakeholder(id, unifiedFileInfo);
        return ResultJson.success();
    }

    /**
     * 更新干系人
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsStakeholder")
    @IdempotentSingle
    public ResultJson updOpsStakeholder(@Validated(UpdGroup.class) OpsStakeholder bo){
        String msg = checkOpsStakeholder(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }
        // 头像处理
        OpsStakeholder db = opsStakeholderService.getById(bo.getId());
        if (db == null){
            return ResultJson.errorCheck();
        }
        DromaraFileStorage[] unifiedFileInfo = dromaraFileStorageService.updFileBefore(bo.getIco(), db.getIco(), bo.getId(), ConstantBase.FileSourceType.STAKEHOLDER_ICO);

        EntityTool.updBase(bo);
        opsStakeholderService.updOpsStakeholder(bo, unifiedFileInfo);
        return ResultJson.success(bo);
    }

    /**
     * 保存干系人
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsStakeholder")
    @IdempotentSingle
    public ResultJson saveOpsStakeholder(@Validated(AddGroup.class) OpsStakeholder bo){
        String msg = checkOpsStakeholder(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }
        if (bo.getSex() == null){
            bo.setSex(ConstantBase.Sex.UNKNOWN);
        }

        bo.setId(IdTool.getId());
        EntityTool.saveBase(bo);

        // 头像处理
        DromaraFileStorage icoFile = dromaraFileStorageService.toProductionBefore(bo.getIco(), bo.getId(), ConstantBase.FileSourceType.STAKEHOLDER_ICO);

        opsStakeholderService.saveOpsStakeholder(bo, icoFile);
        return ResultJson.success(bo);
    }


    /**
     * 干系人数据校验
     * @param bo 干系人 校验数据
     * @return 非null则有错误
     */
    private String checkOpsStakeholder(OpsStakeholder bo){

        return null;
    }

}
